package co.yixiang.huiMin.controller;


import co.yixiang.huiMin.dao.MachinistQueryCriteria;
import co.yixiang.huiMin.dao.TeachingVideoQueryCriteria;
import co.yixiang.huiMin.entity.TeachingVideo;
import co.yixiang.huiMin.entity.UserApply;
import co.yixiang.huiMin.mapper.TeachingVideoMapper;
import co.yixiang.huiMin.repository.TeachingVideoRepository;
import co.yixiang.huiMin.service.TeachingVideoService;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;


/**
 * <p>
 * 教育视频 前端控制器
 * </p>
 *
 * @author Author
 * @since 2020-08-01
 */
@RestController
@Api(tags = "机修学堂视频")
@RequestMapping("/teachingVideo")
public class TeachingVideoController {

    @Resource
    private TeachingVideoService teachingVideoService;
    @Resource
    private TeachingVideoRepository teachingVideoRepository;
    @GetMapping
    @ApiOperation("查询视频")
    public ResponseEntity<Object> inquiryTeachingVideo(Pageable pageable){
        TeachingVideoQueryCriteria teachingVideoQueryCriteria = new TeachingVideoQueryCriteria();
        teachingVideoQueryCriteria.setTeachingVideoIsDel(false);
        Page<TeachingVideo> teachingVideos = teachingVideoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, teachingVideoQueryCriteria, criteriaBuilder), pageable);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("message", PageUtil.toPage(teachingVideos));
        stringObjectHashMap.put("code",200);
        return new ResponseEntity<>(stringObjectHashMap, HttpStatus.OK);
    }
    @PutMapping
    @ApiOperation("添加视频")
    public ResponseEntity<Object> putTeachingVideo(@RequestBody TeachingVideo teachingVideo){

        HashMap<String, Object> resultMap = new HashMap<>();
        if (teachingVideoService.save(teachingVideo)) {
            resultMap.put("code",200);
            resultMap.put("message","添加成功");
            return ResponseEntity.ok(resultMap);
        }
        resultMap.put("code",300);
        resultMap.put("message","添加失败");
        return ResponseEntity.ok(resultMap);

    }
    @PostMapping
    @ApiOperation("修改视频")
    public ResponseEntity<Object> postTeachingVideo(@RequestBody TeachingVideo teachingVideo){
        HashMap<String, Object> resultMap = new HashMap<>();
        if (teachingVideoService.updateById(teachingVideo)) {
            resultMap.put("code",200);
            resultMap.put("message","修改成功");
            return ResponseEntity.ok(resultMap);
        }
        resultMap.put("code",300);
        resultMap.put("message","修改失败");
        return ResponseEntity.ok(resultMap);
    }
    @DeleteMapping
    @ApiOperation("删除视频")
    public ResponseEntity<Object> delTeachingVideo(Integer teachingVideoId){

        HashMap<String, Object> resultMap = new HashMap<>();
        TeachingVideo teachingVideo = new TeachingVideo();
        teachingVideo.setTeachingVideoId(teachingVideoId);
        teachingVideo.setTeachingVideoIsDel(true);
        if (teachingVideoService.updateById(teachingVideo)) {
            resultMap.put("code",200);
            resultMap.put("message","修改成功");
            return ResponseEntity.ok(resultMap);
        }
        resultMap.put("code",300);
        resultMap.put("message","修改失败");
        return ResponseEntity.ok(resultMap);
    }
}

